﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using FanaticToy.Core;
using FanaticToy.Core.Configure;

namespace FanaticToy.Test
{



    ///<summary>测试路径。</summary>
    internal class Program
    {

        public string Name { get => _name; set => _name = value; }
        private string _name;

        public string Age { get; set; }

        static void Main2(string[] args)
        {
            var slnPath = @"D:\Code\FanaticToyExmaple\FanaticToyExmaple.sln";
            var manager = CreateDirectoryManager(slnPath);
            var dtoMetaDatas = manager.GetCodeMetaDatas("dto", "user", "login");
            var controllerMetaData = manager.GetCodeMetaData("controller", "user", "login");
            var serviceMetaData = manager.GetCodeMetaData("service", "user", "login");

            foreach (var metaData in dtoMetaDatas)
            {
                Console.WriteLine(metaData);
            }

            Console.WriteLine(controllerMetaData);
            Console.WriteLine(serviceMetaData);

            Console.ReadKey();
        }

        static void Main(string[] args)
        {
            var dte = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE");

            //var quick = new SingletonCreator();
            //quick.Execute(dte);

            var extractor = new Core.AnaemiaExtractor.Extractor();
            var extractorCode = extractor.Extract(@"D:\Code\FanaticToy\FanaticToy.Test\Program.cs");
            Console.WriteLine(extractorCode);

            Console.ReadKey();
        }

        ///<summary>获得项目所属名。</summary>
        private static string GetProjectName(DTE dte)
        {
            var activeDocument = dte.ActiveDocument;
            var projItem = activeDocument.ProjectItem;
            var containingProject = projItem.ContainingProject;

            if (containingProject == null) return string.Empty;

            return containingProject.Name;
        }

        ///<summary>获得项目所属名。</summary>
        private static string GetProjectPath(DTE dte)
        {
            var activeDocument = dte.ActiveDocument;
            var projItem = activeDocument.ProjectItem;
            var containingProject = projItem.ContainingProject;

            if (containingProject == null) return string.Empty;

            return Path.GetDirectoryName(containingProject.FileName);
        }

        private static string GetDocumentPath(DTE dte)
        {
            var activeDocument = dte.ActiveDocument;
            return activeDocument.FullName;

        }
        static void Main3(string[] args)
        {
            var slnPath = @"D:\Code\FanaticToyExmaple\FanaticToyExmaple.sln";

            for (int i = 33; i <= 49; i++)
            {
                var method = Core.Utils.MethodMetaUtils.GetMethodNameByLineId(@"D:\Code\FanaticToy\FanaticToy.Test\Program.cs", i);
                Console.WriteLine($"{i,4} {method}");
            }

            //var method2 =  Core.Utils.MethodMetaUtils.GetMethodName(@"D:\Code\FanaticToy\FanaticToy.Test\Program.cs", 36);

            Console.ReadKey();
        }

        static void Main4(string[] args)
        {
            //DTE dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE");
            //Console.WriteLine(dte.ActiveDocument.FullName);
            //var item = dte.ActiveDocument.ProjectItem;
            //var textdoc = dte.ActiveDocument.Object("TextDocument") as TextDocument;
            //var line = textdoc.Selection.CurrentLine;
            //Console.WriteLine(line);
            //Console.ReadKey();

            var dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE");
            Console.WriteLine(dte.Solution.FullName);
            Console.ReadKey();

        }

        ///<summary>创建目录管理器工具。</summary>
        private static DirectoryManager CreateDirectoryManager(string slnPath)
        {
            var config = CreateConfig();
            var directoryManager = new DirectoryManager(slnPath, config);
            return directoryManager;
        }

        private static DirectoryManager CreateDirectoryManagerByConfig(string slnPath)
        {
            var directoryManager = new DirectoryManager(slnPath);
            return directoryManager;
        }

        private static ToyConfiguration CreateConfig()
        {
            var serviceId = "service";
            var controllerId = "controller";
            var dtoId = "dto";

            var servicePath = "FanaticToyExmaple\\Services";
            var controllerPath = "FanaticToyExmaple\\Controllers";
            var dtoPath = "FanaticToyExmaple\\Dto";

            var toyConfig = new ToyConfiguration();
            toyConfig.OrderList.Add(serviceId);
            toyConfig.OrderList.Add(controllerId);
            toyConfig.OrderList.Add(dtoId);

            var serviceNode = new ToyNode()
            {
                Name = serviceId,
                Strategy = new FileSystemLevel1FormatToyStrategy()
                {
                    EndFlagList = new List<string>()
                     {
                         "Service"
                     }
                },
                ReleativePath = servicePath,
                LinkNodes = new List<ToyNodeTarget>
                {
                    new ToyNodeTarget(controllerId, LinkType.MemberToMember),
                    new ToyNodeTarget(dtoId, LinkType.MemberToFile),
                }
            };

            var controllerNode = new ToyNode()
            {
                Name = controllerId,
                Strategy = new FileSystemLevel1FormatToyStrategy()
                {
                    EndFlagList = new List<string>()
                     {
                         "Controller"
                     }
                },
                ReleativePath = controllerPath,
                LinkNodes = new List<ToyNodeTarget>
                {
                    new ToyNodeTarget(serviceId, LinkType.MemberToMember),
                    new ToyNodeTarget(dtoId, LinkType.MemberToFile),
                }
            };

            var dtoNode = new ToyNode()
            {
                Name = dtoId,
                Strategy = new FileSystemLevel2FormatToyStrategy()
                {
                    EndFlagList = new List<string>()
                    {
                        "ResponseDto",
                        "RequestDto"
                    },
                },
                ReleativePath = dtoPath,
                LinkNodes = new List<ToyNodeTarget>
                {
                    new ToyNodeTarget(serviceId, LinkType.FileToMember),
                    new ToyNodeTarget(controllerId, LinkType.FileToMember),
                }
            };

            toyConfig.Nodes.Add(serviceNode);
            toyConfig.Nodes.Add(controllerNode);
            toyConfig.Nodes.Add(dtoNode);

            return toyConfig;
        }

    }
}
